package com.zjcloud.motr.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import com.zjcloud.common.utils.DateUtils;
import com.zjcloud.motr.domain.ZjyTimingType;
import com.zjcloud.motr.mapper.ZjyTimingTypeMapper;
import com.zjcloud.motr.vo.ZjyStaffTimingMonthVO;
import com.zjcloud.motr.vo.ZjyStaffTimingVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zjcloud.motr.mapper.ZjyStaffTimingMapper;
import com.zjcloud.motr.domain.ZjyStaffTiming;
import com.zjcloud.motr.service.IZjyStaffTimingService;

/**
 * 人员记时Service业务层处理
 * 
 * @author wxl
 * @date 2024-09-24
 */
@Service
public class ZjyStaffTimingServiceImpl implements IZjyStaffTimingService 
{
    @Autowired
    private ZjyStaffTimingMapper zjyStaffTimingMapper;

    @Autowired
    private ZjyTimingTypeMapper zjyTimingTypeMapper;

    /**
     * 查询人员记时
     * 
     * @param id 人员记时主键
     * @return 人员记时
     */
    @Override
    public ZjyStaffTiming selectZjyStaffTimingById(Long id)
    {
        return zjyStaffTimingMapper.selectZjyStaffTimingById(id);
    }

    /**
     * 查询人员记时列表
     * 
     * @param zjyStaffTiming 人员记时
     * @return 人员记时
     */
    @Override
    public List<ZjyStaffTiming> selectZjyStaffTimingList(ZjyStaffTiming zjyStaffTiming)
    {
        return zjyStaffTimingMapper.selectZjyStaffTimingList(zjyStaffTiming);
    }

    @Override
    public List<ZjyStaffTimingVO> selectZjyStaffTimingListVO(ZjyStaffTiming zjyStaffTiming)
    {
        List<ZjyStaffTiming> zjyStaffTimingVOS = zjyStaffTimingMapper.selectZjyStaffTimingList(zjyStaffTiming);


        System.out.println("zjyStaffTimingVOS = " + zjyStaffTimingVOS);

        List<ZjyStaffTimingVO> zjyStaffTimingList = new ArrayList<>();

        zjyStaffTimingVOS.forEach(zjyStaff -> {
            ZjyTimingType zjyTimingType = zjyTimingTypeMapper.selectZjyTimingTypeById(zjyStaff.getTimingTypeId());

            ZjyStaffTimingVO zjyStaffTimingVO = new ZjyStaffTimingVO();

            System.out.println("zjyTimingType = " + zjyTimingType);


            zjyStaffTimingVO.setId(zjyStaff.getId());
            zjyStaffTimingVO.setName(zjyStaff.getName());
            zjyStaffTimingVO.setCreateTime(zjyStaff.getCreateTime());
            zjyStaffTimingVO.setHoursWorked(zjyStaff.getHoursWorked());
            zjyStaffTimingVO.setTimingName(zjyTimingType.getName());
            zjyStaffTimingVO.setHourlyRate(zjyTimingType.getHourlyRate());

            zjyStaffTimingList.add(zjyStaffTimingVO);
        });

        return zjyStaffTimingList;
    }

    /**
     * 新增人员记时
     * 
     * @param zjyStaffTiming 人员记时
     * @return 结果
     */
    @Override
    public int insertZjyStaffTiming(ZjyStaffTiming zjyStaffTiming)
    {
        zjyStaffTiming.setCreateTime(DateUtils.getNowDate());
        return zjyStaffTimingMapper.insertZjyStaffTiming(zjyStaffTiming);
    }

    /**
     * 修改人员记时
     * 
     * @param zjyStaffTiming 人员记时
     * @return 结果
     */
    @Override
    public int updateZjyStaffTiming(ZjyStaffTiming zjyStaffTiming)
    {
        zjyStaffTiming.setUpdateTime(DateUtils.getNowDate());
        return zjyStaffTimingMapper.updateZjyStaffTiming(zjyStaffTiming);
    }

    /**
     * 批量删除人员记时
     * 
     * @param ids 需要删除的人员记时主键
     * @return 结果
     */
    @Override
    public int deleteZjyStaffTimingByIds(Long[] ids)
    {
        return zjyStaffTimingMapper.deleteZjyStaffTimingByIds(ids);
    }

    /**
     * 删除人员记时信息
     * 
     * @param id 人员记时主键
     * @return 结果
     */
    @Override
    public int deleteZjyStaffTimingById(Long id)
    {
        return zjyStaffTimingMapper.deleteZjyStaffTimingById(id);
    }

    @Override
    public List<ZjyStaffTimingMonthVO> selectZjyStaffTimingMonthListVO(Date month) {
        ZjyStaffTiming zjyStaffTiming = new ZjyStaffTiming();
        List<ZjyStaffTiming> zjyStaffTimingVOS = zjyStaffTimingMapper.selectZjyStaffTimingList(zjyStaffTiming);

        List<ZjyStaffTimingMonthVO> zjyStaffTimingList = new ArrayList<>();

        // 根据 CreateTime 遍历过滤 month 月份数据
        zjyStaffTimingVOS.forEach(zjyStaff -> {
            // 使用Calendar替代过时的Date.getYear()和Date.getMonth()方法
            Calendar cal1 = Calendar.getInstance();
            cal1.setTime(zjyStaff.getCreateTime());
            Calendar cal2 = Calendar.getInstance();
            cal2.setTime(month);
            
            int year1 = cal1.get(Calendar.YEAR);
            int year2 = cal2.get(Calendar.YEAR);
            int month1 = cal1.get(Calendar.MONTH) + 1;
            int month2 = cal2.get(Calendar.MONTH) + 1;

            if (year1 == year2 && month1 == month2) {
                ZjyTimingType zjyTimingType = zjyTimingTypeMapper.selectZjyTimingTypeById(zjyStaff.getTimingTypeId());

                ZjyStaffTimingMonthVO zjyStaffTimingVO = new ZjyStaffTimingMonthVO();
                BigDecimal hourlyRate = zjyTimingType.getHourlyRate();
                BigDecimal hoursWorked = zjyStaff.getHoursWorked();
                SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
                BigDecimal total = hourlyRate.multiply(hoursWorked);


                zjyStaffTimingVO.setId(zjyStaff.getId());
                zjyStaffTimingVO.setDailyTotal(total);
                zjyStaffTimingVO.setMonth(sdf.format(zjyStaff.getCreateTime()));
                zjyStaffTimingVO.setName(zjyStaff.getName());
                zjyStaffTimingVO.setCreateTime(zjyStaff.getCreateTime());
                zjyStaffTimingVO.setHoursWorked(zjyStaff.getHoursWorked());
                zjyStaffTimingVO.setTimingName(zjyTimingType.getName());
                zjyStaffTimingVO.setHourlyRate(zjyTimingType.getHourlyRate());

                zjyStaffTimingList.add(zjyStaffTimingVO);
            }
        });


        return zjyStaffTimingList;
    }
}
